package edu.nuc.projdemo.sys.dao.impl;

import java.util.List;

import edu.nuc.projdemo.common.DBUnitHelper;
import edu.nuc.projdemo.common.PageControl;
import edu.nuc.projdemo.common.Pager;
import edu.nuc.projdemo.model.SysFunction;
import edu.nuc.projdemo.model.Sysuser;
import edu.nuc.projdemo.sys.dao.interfaces.IUserDao;

public class UserDao implements IUserDao {

	public Sysuser login(Sysuser user) {

		String sql = "SELECT USERID,A.ROLEID,USERNAME,USERPWD,USERTRUENAME,USERSTATE, "
				+ "B.ROLENAME FROM SYSUSER A "
				+ "INNER JOIN SYSROLE B ON A.ROLEID=B.ROLEID "
				+ "WHERE USERSTATE=1 AND USERNAME=? AND USERPWD=? ";
		List<Sysuser> list = DBUnitHelper.executeQuery(sql, Sysuser.class, user
				.getUsername(), user.getUserpwd());
		if (list.size() > 0) {
			user = list.get(0);
		} else {
			user = null;
		}
		return user;
	}

	public List<SysFunction> initpage(Sysuser user) {

		List<SysFunction> list = null;

		if (user.getRoleid().equals(-1)) {
			String sql = "SELECT A.FUNID,A.FUNNAME,A.FUNURL,A.FUNPID FROM SYSFUNCTION A WHERE A.FUNSTATE=1";
			list = DBUnitHelper.executeQuery(sql, SysFunction.class);
		} else {
			String sql = "SELECT A.FUNID,A.FUNNAME,A.FUNURL,A.FUNPID FROM SYSFUNCTION A "
					+ "INNER JOIN ROLERIGHT B ON A.FUNID=B.FUNID WHERE B.ROLEID=? AND A.FUNSTATE=1";
			list = DBUnitHelper.executeQuery(sql, SysFunction.class, user
					.getRoleid());
		}
		return list;
	}

	public Pager<Sysuser> list(Sysuser user, PageControl pc) {
		
		Pager<Sysuser> pager = null;
		
		String sql = "SELECT USERID,ROLEID,USERNAME,USERTRUENAME,USERSTATE FROM SYSUSER " +
				     "WHERE ROLEID > 0 AND USERSTATE = 1";
		
		
		if(user.getRolename()!=null){
			
			sql += " AND USERNAME = ?";
			pager = DBUnitHelper.execlist(sql, pc, Sysuser.class, user.getUsername());
			
		}else{
			
			
			pager = DBUnitHelper.execlist(sql, pc, Sysuser.class,null);
			
		}
		
		return pager;
	}

	public Integer add(Sysuser user) {
		
		String sql = "INSERT INTO SYSUSER(USERNAME," +
				"USERPWD,USERTRUENAME,USERSTATE,ROLEID) VALUES(?,?,?,?,?)";
		
		
		return DBUnitHelper.executeUpdate(sql, user.getUsername(),user.getUserpwd(),user.getUsertruename(),
				user.getUserstate(),user.getRoleid());
		
	}

	public Sysuser findUser(Sysuser user) {
		
		String sql = "SELECT USERID,ROLEID,USERNAME,USERPWD,USERTRUENAME," +
				"USERSTATE FROM SYSUSER WHERE USERID=?";
		
		 List<Sysuser> list = DBUnitHelper.executeQuery(sql, Sysuser.class, user.getUserid());
		
		
		return list.get(0);
	}

	public Integer editUser(Sysuser user) {
		
		String sql ="UPDATE SYSUSER SET USERNAME=?," +
				"USERTRUENAME=?,USERPWD=?,ROLEID=?,USERSTATE=? WHERE userid=?";
		
		return DBUnitHelper.executeUpdate(sql, user.getUsername(),user.getUsertruename(),
				user.getUserpwd(),user.getRoleid(),user.getUserstate(),user.getUserid());
		
		
	}

	public Integer modifypwd(String userpwd, String newpwd, String username) {
		
		String sql = "UPDATE SYSUSER SET USERPWD=? WHERE USERNAME=? AND USERPWD=?";
		
		return DBUnitHelper.executeUpdate(sql, newpwd,username,userpwd);
	}

}
